﻿using System;
using System.IO;

namespace Mono.Cecil
{
	// Token: 0x02000061 RID: 97
	public sealed class EmbeddedResource : Resource
	{
		// Token: 0x17000064 RID: 100
		// (get) Token: 0x060003A1 RID: 929 RVA: 0x0001AEE0 File Offset: 0x000190E0
		public override ResourceType ResourceType
		{
			get
			{
				return ResourceType.Embedded;
			}
		}

		// Token: 0x060003A2 RID: 930 RVA: 0x000034CF File Offset: 0x000016CF
		public EmbeddedResource(string name, ManifestResourceAttributes attributes, byte[] data) : base(name, attributes)
		{
			this.data = data;
		}

		// Token: 0x060003A3 RID: 931 RVA: 0x000034E2 File Offset: 0x000016E2
		public EmbeddedResource(string name, ManifestResourceAttributes attributes, Stream stream) : base(name, attributes)
		{
			this.stream = stream;
		}

		// Token: 0x060003A4 RID: 932 RVA: 0x000034F5 File Offset: 0x000016F5
		internal EmbeddedResource(string name, ManifestResourceAttributes attributes, uint offset, MetadataReader reader) : base(name, attributes)
		{
			this.offset = new uint?(offset);
			this.reader = reader;
		}

		// Token: 0x060003A5 RID: 933 RVA: 0x0001AEF4 File Offset: 0x000190F4
		public Stream GetResourceStream()
		{
			bool flag = this.stream != null;
			Stream result;
			if (flag)
			{
				result = this.stream;
			}
			else
			{
				bool flag2 = this.data != null;
				if (flag2)
				{
					result = new MemoryStream(this.data);
				}
				else
				{
					bool flag3 = this.offset != null;
					if (!flag3)
					{
						throw new InvalidOperationException();
					}
					result = new MemoryStream(this.reader.GetManagedResource(this.offset.Value));
				}
			}
			return result;
		}

		// Token: 0x060003A6 RID: 934 RVA: 0x0001AF68 File Offset: 0x00019168
		public byte[] GetResourceData()
		{
			bool flag = this.stream != null;
			byte[] result;
			if (flag)
			{
				result = EmbeddedResource.ReadStream(this.stream);
			}
			else
			{
				bool flag2 = this.data != null;
				if (flag2)
				{
					result = this.data;
				}
				else
				{
					bool flag3 = this.offset != null;
					if (!flag3)
					{
						throw new InvalidOperationException();
					}
					result = this.reader.GetManagedResource(this.offset.Value);
				}
			}
			return result;
		}

		// Token: 0x060003A7 RID: 935 RVA: 0x0001AFD8 File Offset: 0x000191D8
		private static byte[] ReadStream(Stream stream)
		{
			bool canSeek = stream.CanSeek;
			byte[] result;
			if (canSeek)
			{
				int num = (int)stream.Length;
				byte[] array = new byte[num];
				int num2 = 0;
				int num3;
				while ((num3 = stream.Read(array, num2, num - num2)) > 0)
				{
					num2 += num3;
				}
				result = array;
			}
			else
			{
				byte[] array2 = new byte[8192];
				MemoryStream memoryStream = new MemoryStream();
				int num3;
				while ((num3 = stream.Read(array2, 0, array2.Length)) > 0)
				{
					memoryStream.Write(array2, 0, num3);
				}
				result = memoryStream.ToArray();
			}
			return result;
		}

		// Token: 0x040000E6 RID: 230
		private readonly MetadataReader reader;

		// Token: 0x040000E7 RID: 231
		private uint? offset;

		// Token: 0x040000E8 RID: 232
		private byte[] data;

		// Token: 0x040000E9 RID: 233
		private Stream stream;
	}
}
